package top.huanyv.tools.bean;

/**
 * @author huanyv
 * @date 2022/12/25 14:22
 */
public class StopWatch {

    private long startVal = 0;

    private long stopVal = 0;

    private String name;

    public StopWatch() {
    }

    public StopWatch(String name) {
        this.name = name;
    }

    public StopWatch start() {
        this.startVal = System.currentTimeMillis();
        return this;
    }

    public void stop() {
        this.stopVal = System.currentTimeMillis();
        checkIsStart();
    }

    public long getSeconds() {
        checkIsStart();
        checkIsStop();
        return (this.stopVal - this.startVal) / 1000;
    }

    public long getMilliSeconds() {
        checkIsStart();
        checkIsStop();
        return this.stopVal - this.startVal;
    }

    public double getPreciseSeconds() {
        checkIsStart();
        checkIsStop();
        return (this.stopVal - this.startVal) / 1000.0;
    }

    public void checkIsStart() {
        if (startVal == 0) {
            throw new IllegalArgumentException("'" + this.name + "' stopwatch did not start!");
        }
    }

    public void checkIsStop() {
        if (stopVal == 0) {
            throw new IllegalArgumentException("'" + this.name + "' stopwatch did not stop!");
        }
    }

}
